<?php

namespace App\Http\ViewComposers;

use App\Handlers\role;
use Illuminate\View\View;
use App\Services\Config\ChannelsService;
use Illuminate\Support\Facades\Auth;

class ChannelComposer
{
    protected $channelService;

    public function __construct(ChannelsService $channelsService)
    {
        $this->channelsService = $channelsService;
    }

    public function compose(View $view)
    {
        $reqChannelId = $view->reqChannelId;
        $reqUniqueId = $view->reqUniqueId;
        $channels = Auth::guard('admin')->user()->getChannels(['game_id' => session('game_id')], 'channel_id');

        //拼出 渠道table
        $str_arr_channels_id = '';
        $channelHtml = '';
        foreach ($channels as $channelId => $channelName) {
            $checked = '';


            if (empty($reqChannelId) || in_array($channelId, $reqChannelId)) {
                $checked = 'checked="checked"';
                $str_arr_channels_id .= $channelId . ',';
            }

            $channelHtml .= <<<EOF
<div class="col-sm-1" style="min-width: 20rem">
    <label>&emsp;$channelName
            <input type="checkbox" class="pull-left" $checked name="channel_id[]" value="$channelId">
    </label>
</div>
EOF;
        }
        $str_arr_channels_id = (empty($str_arr_channels_id) ? '' : substr($str_arr_channels_id, 0, -1));

        $view->with(compact('str_arr_channels_id', 'channelHtml'));
    }
}